package chw412;

import java.util.*;

import pacworld.VisibleAgent;

public class KnownAgent implements Comparable<KnownAgent>,
		Comparator<KnownAgent> {
	private String id;
	private int x = 0;
	private int y = 0;

	public enum agent_state {
		INTENT, // the agent is pursuing some certain target package
		CARRY, // the agent is carrying some package
		EXPLORE, // the agent is exploring the unknown area in the world, by default, agents are exploring.
		IDLE
	};
	
	private agent_state state;

	public KnownAgent(String id, int x, int y) {
		this.id = id;
		this.x = x;
		this.y = y;
		this.state = agent_state.IDLE;
	}

	public KnownAgent(VisibleAgent va) {
		this.id = va.getId();
		this.x = va.getX();
		this.y = va.getY();
		this.state = agent_state.IDLE;
	}

	public agent_state get_state() {
		return state;
	}

	/** Get the unique string id for the agent. */
	public String getId() {
		return id;
	}

	/** Get the X location of the agent. */
	public int getX() {
		return x;
	}

	/** Get the Y location of the agent. */
	public int getY() {
		return y;
	}

	public boolean update_state(agent_state s) {
		state = s;
		return true;
	}

	public void update_loc(int x, int y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * Tests if this object is equivalent to another object by comparing the
	 * id's of the agents.
	 */
	public boolean equals(Object obj) {

		KnownAgent otherAgent;

		if (obj instanceof KnownAgent) {
			otherAgent = (KnownAgent) obj;
			if (id.equals(otherAgent.getId()))
				return true;
		}
		return false;
	}

	public String toString() {
		String str = "Agent " + id + ": Loc=(" + x + ", " + y + ")";
		switch(state){
		case INTENT:
			str += "\tAPPROACH";
			break;
		case CARRY:
			str += "\tCARRY";
			break;
		case EXPLORE:
			str += "\tEXPLORE";
			break;
		case IDLE:
			str += "\tIDLE";
			break;			
		default:
			assert(false);
			break;
		}
		return str;
	}

	@Override
	public int compare(KnownAgent o1, KnownAgent o2) {
		// TODO Auto-generated method stub
		return Integer.parseInt(o1.getId())
				- Integer.parseInt(((KnownAgent) o2).getId());
	}

	@Override
	public int compareTo(KnownAgent o) {
		if (Integer.parseInt(this.id) < Integer.parseInt(((KnownAgent) o)
				.getId()))
			return -1;
		else if (Integer.parseInt(this.id) == Integer.parseInt(((KnownAgent) o)
				.getId()))
			return 0;
		else
			return 1;
	}
}